#include "commands.hpp"
#include "geometry.hpp"
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>

// 查询数据
void queryData(const Dataset& dataset) {
    std::cout << "\nPoint 数量: " << dataset.points.size() << std::endl;
    for (const auto& point : dataset.points) {
        std::cout << "ID号: " << point.id 
                  << " 坐标x: " << std::fixed << std::setprecision(6) << point.coord.x 
                  << " 坐标y: " << point.coord.y << std::endl;
    }

    std::cout << "\nPolyline 数量: " << dataset.polylines.size() << std::endl;
    for (const auto& polyline : dataset.polylines) {
        std::cout << "ID号: " << polyline.id 
                  << " 点数: " << polyline.pointIndices.size() << std::endl;
    }

    std::cout << "\nPolygon 数量: " << dataset.polygons.size() << std::endl;
    for (const auto& polygon : dataset.polygons) {
        std::cout << "ID号: " << polygon.id 
                  << " 点数: " << polygon.pointIndices.size() << std::endl;
    }
}

// 计算并显示面积
void calculateAndDisplayAreas(Dataset& dataset) {
    calculateAllPolygonAreas(dataset);
    
    // 创建临时数组用于排序
    std::vector<std::reference_wrapper<PolygonFeature>> sortedPolygons;
    for (auto& polygon : dataset.polygons) {
        sortedPolygons.push_back(std::ref(polygon));
    }
    
    // 按面积排序
    std::sort(sortedPolygons.begin(), sortedPolygons.end(),
              [](const PolygonFeature& a, const PolygonFeature& b) {
                  return a.area < b.area;
              });
    
    std::cout << "\nPolygon 数量: " << dataset.polygons.size() 
              << " (按照面积升序)" << std::endl;
    for (const auto& polygon : sortedPolygons) {
        std::cout << "ID号: " << polygon.get().id 
                  << " 点数: " << polygon.get().pointIndices.size() 
                  << " 面积: " << std::fixed << std::setprecision(6) 
                  << polygon.get().area << std::endl;
    }
}

// 统计类型
struct TypeCount {
    std::string type;
    size_t count;
};

// 显示统计信息
void displayStatistics(const Dataset& dataset) {
    std::vector<TypeCount> counts = {
        {"polygon", dataset.polygons.size()},
        {"polyline", dataset.polylines.size()},
        {"point", dataset.points.size()}
    };
    
    // 按数量降序排序
    std::sort(counts.begin(), counts.end(),
              [](const TypeCount& a, const TypeCount& b) {
                  return a.count > b.count;
              });
    
    std::cout << "\n数量  类型" << std::endl;
    for (const auto& count : counts) {
        std::cout << std::left << std::setw(6) << count.count 
                  << " " << count.type << std::endl;
    }
} 